package com.longxi.tianjintianbao.greedao.controller;


import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.greedao.dao.InventoryChildDao;
import com.longxi.tianjintianbao.greedao.dao.InventoryDao;
import com.longxi.tianjintianbao.greedao.dao.PatrolDao;
import com.longxi.tianjintianbao.greedao.entity.Inventory;
import com.longxi.tianjintianbao.greedao.entity.InventoryChild;
import com.longxi.tianjintianbao.greedao.entity.Patrol;
import com.longxi.tianjintianbao.greedao.entity.PatrolContent;
import com.longxi.tianjintianbao.utils.LogUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 70339 on 2018/5/16.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class DataInventoryController {

    private static InventoryDao mInventoryDao = MyApplication.getInstance().getDaoSession().getInventoryDao();
    private static InventoryChildDao mInventoryChildDao = MyApplication.getInstance().getDaoSession().getInventoryChildDao();
    /**
     * 增加盘点儿子
     */
    public static InventoryChild insertInventoryChildDao(InventoryChild inventoryChild)
    {
        InventoryChild oldInventoryChild = mInventoryChildDao.queryBuilder()
                .where(InventoryChildDao.Properties.Iid.eq(inventoryChild.getIid()))
                .where(InventoryChildDao.Properties.Number.eq(inventoryChild.getNumber()))
                .unique();
        if(oldInventoryChild != null){
            return oldInventoryChild;
        }
        mInventoryChildDao.insertOrReplace(inventoryChild);
        return inventoryChild;
    }

    /**
     * 入库前先对数据进行查询
     */
    public static InventoryChild insertInventoryChildDao(long iid, String number)
    {
        InventoryChild inventoryChild = mInventoryChildDao.queryBuilder()
                .where(InventoryChildDao.Properties.Iid.eq(iid))
                .where(InventoryChildDao.Properties.Number.eq(number))
                .unique();
        if(inventoryChild != null){
            return inventoryChild;
        }
        return null;
    }

    /**
     * 根据资产名称 资产编号 关键字查询数据
     */
    public static List<InventoryChild> findByNameOrNumber(long iid, String key, int page, int limit)
    {
        List<InventoryChild> inventoryChild = mInventoryChildDao.queryBuilder()
                .where(InventoryChildDao.Properties.Iid.eq(iid))
                .whereOr(InventoryChildDao.Properties.Number.like("%" + key + "%"), InventoryChildDao.Properties.Name.like("%" + key + "%"))
                .offset(page * limit).limit(limit)
                .list();
        return inventoryChild;
    }

    /**
     * 增加盘点爸爸
     */
    public static Inventory insertInventory(Inventory inventory)
    {
        Inventory oldInventory = mInventoryDao.queryBuilder()
                .where(InventoryDao.Properties.Number.eq(inventory.getNumber()))
                .unique();
        if(oldInventory != null){
            return oldInventory;
        }
        mInventoryDao.insertOrReplace(inventory);
        return inventory;
    }

    /**
     * 查巡盘点儿子全部数据
     */
//    public static List<InventoryChild> SearchPatrolContent(Inventory inventory)
//    {
//        return inventory.getInventorychild();//查找盘点儿子则需要通过盘点爸爸表来获取盘点儿子的集合然后再遍历找到所需盘点表
//    }

    public static void SearchInventoryChild(){
        List<InventoryChild> list = mInventoryChildDao.queryBuilder()
                .orderAsc(InventoryChildDao.Properties.Id)
                .list();
        LogUtils.d("SearchInventoryChild", list.size() + "");
    }

    /**
     * 查巡盘点儿子分页还根据状态
     */
    public static List<InventoryChild> SearchInventoryChild(Inventory inventory, int page, int limit, int position)
    {
        String isdraft;
        String state;
        List<InventoryChild> list = new ArrayList<>();
        switch (position) {
            case 0:
                list = mInventoryChildDao.queryBuilder()
                        .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                        .where(InventoryChildDao.Properties.State.eq("0"))
                        .offset(page * limit).limit(limit)
                        .orderAsc(InventoryChildDao.Properties.Id)
                        .list();
                break;
            case 1:
                isdraft = "0";
                state = "0";
                list = mInventoryChildDao.queryBuilder()
                        .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                        .where(InventoryChildDao.Properties.Isdraft.eq(isdraft))
                        .where(InventoryChildDao.Properties.State.eq(state))
                        .offset(page * limit).limit(limit)
                        .orderAsc(InventoryChildDao.Properties.Id)
                        .list();
                break;
            case 2:
                isdraft = "0";
                state = "0";
                list = mInventoryChildDao.queryBuilder()
                        .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                        .where(InventoryChildDao.Properties.Isdraft.notEq(isdraft))
                        .where(InventoryChildDao.Properties.State.eq(state))
                        .orderAsc(InventoryChildDao.Properties.Isdraft)
                        .offset(page * limit).limit(limit)
                        .list();
                break;
            case 3:
                state = "1";
                list = mInventoryChildDao.queryBuilder()
                        .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                        .where(InventoryChildDao.Properties.State.eq(state))
                        .offset(page * limit).limit(limit)
                        .orderAsc(InventoryChildDao.Properties.Id)
                        .list();
                break;
        }

        return list;
    }

    /**
     * 查巡盘点儿子不同分类下个数
     */
    public static long SearchInventoryChildCount(Inventory inventory, int position)
    {
        String isdraft;
        String state;
        long count = 0;
        switch (position) {
            case 0:
                count = mInventoryChildDao.queryBuilder()
                        .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                        .where(InventoryChildDao.Properties.State.eq("0"))
                        .count();
                break;
            case 1:
                isdraft = "0";
                state = "0";
                count = mInventoryChildDao.queryBuilder()
                        .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                        .where(InventoryChildDao.Properties.Isdraft.eq(isdraft))
                        .where(InventoryChildDao.Properties.State.eq(state))
                        .count();
                break;
            case 2:
                isdraft = "0";
                state = "0";
                count = mInventoryChildDao.queryBuilder()
                        .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                        .where(InventoryChildDao.Properties.Isdraft.notEq(isdraft))
                        .where(InventoryChildDao.Properties.State.eq(state))
                        .count();
                break;
            case 3:
                state = "1";
                count = mInventoryChildDao.queryBuilder()
                        .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                        .where(InventoryChildDao.Properties.State.eq(state))
                        .count();
                break;
        }

        return count;
    }

    /**
     * 查巡盘点儿子分页还根据状态
     */
    public static List<Inventory> SearchInventory(int page, int limit)
    {
        //惰性加载
        List<Inventory> list = mInventoryDao.queryBuilder()
                .offset(page * limit).limit(limit)
                .orderAsc(InventoryDao.Properties.Id)
                .list();
        return list;
    }

    /**
     * 查巡盘点儿子是否还有需要上传的数据
     */
    public static long SearchInventoryByIsdraft(Inventory inventory)
    {
        long count = mInventoryChildDao.queryBuilder()
                .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                .where(InventoryChildDao.Properties.Isdraft.notEq("2"))
                .count();
        return count;
    }

    /**
     * 查询孩儿他爸
     * @param number//单号
     * @return
     */
    public static Inventory getPatrolByBillid(String number){
        Inventory inventory = mInventoryDao.queryBuilder()
                .where(InventoryDao.Properties.Number.eq(number))
                .unique();
        return inventory;
    }

    /**
     * 查询孩儿他爸
     * @param iid//单号
     * @return
     */
    public static Inventory getPatrolByIid(long iid){
        Inventory inventory = mInventoryDao.queryBuilder()
                .where(InventoryDao.Properties.Id.eq(iid))
                .unique();
        return inventory;
    }

    /**
     * 查询孩儿info
     * @param id//单号
     * @return
     */
    public static InventoryChild getInventoryChildById(long id){
        InventoryChild inventoryChild = mInventoryChildDao.queryBuilder()
                .where(InventoryChildDao.Properties.Id.eq(id))
                .unique();
        return inventoryChild;
    }

    /**
     * 判断该用户是否有需要上传的数据
     * @return
     */
    public static long isUploadData(Inventory inventory, int position){
        String state = "";
        switch (position) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                state = "0";
                break;
            case 3:
                state = "1";
                break;
        }

        String[] isdraft = {"1", "3"};

        long count = mInventoryChildDao.queryBuilder()
                .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                .where(InventoryChildDao.Properties.State.eq(state))
                .where(InventoryChildDao.Properties.Isdraft.in(isdraft))
                .count();
        return count;
    }

    /**
     * 获取需要上传的数据
     * @return
     */
    public static List<InventoryChild> getUploadList(Inventory inventory, int position){
        String state = "";
        switch (position) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                state = "0";
                break;
            case 3:
                state = "1";
                break;
        }
        String[] isdraft = {"1", "3"};
        List<InventoryChild> list = mInventoryChildDao.queryBuilder()
                .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                .where(InventoryChildDao.Properties.State.eq(state))
                .where(InventoryChildDao.Properties.Isdraft.in(isdraft))
                .offset(0).limit(99)
                .list();
        return list;
    }

    /**
     *修改某条盘点儿子信息
     */
    public static void correctSportInfo(InventoryChild info)
    {
        mInventoryChildDao.update(info);
    }

    public static void DeleteInventoryAndChild(Inventory inventory) {
        mInventoryDao.delete(inventory);
        List<InventoryChild> list = mInventoryChildDao.queryBuilder()
                .where(InventoryChildDao.Properties.Iid.eq(inventory.getId()))
                .list();
        if(list != null){
            mInventoryChildDao.deleteInTx(list);
        }
    }

    public static void DeleteInventoryChild(InventoryChild inventoryChild) {
        mInventoryChildDao.delete(inventoryChild);
    }

//    public static void updateAllByIsDraft(){
//        String sql ="update PATROL set IS_DRAFT = '2'" +
//                " where USERID = " + MyApplication.loginUser.userid
//                + " and IPTYPE = " + MyApplication.IPID
//                + " and IS_DRAFT = '1'" ;
//        MyApplication.db.execSQL(sql);
//    }

}
